home *** CD-ROM | disk | FTP | other *** search
Wrap
Java Source | 2001-06-23 | 12.8 KB | 433 lines
/* Interaction Frame by Scott Ziegler 08.19.99 This file and its intellectual contents are considered property of the creator and are to be treated as such with respect to use in other applications. Any use of this software for any purpose whatsoever must have explicit permission from the author of the file. This code is part of an ongoing development process for future possible products, and so is considered private property. Do not use without permission. Author: Scott C. Ziegler <Aslan@Narnia.net> Instructions for use: This program is used as an interface by which to textually control programs on a prompt-response basis. The following functions provide the behaviors to integrate its use with user-defined classes, allowing for testing and use. The functions: String getLine() - waits for user input to be entered at the prompt, and returns the entered string. void printLine(String s) - prints the string s and provides a newline ("\n") void printEndLine(String s) - prints the string s and gives a newline and a user prompt. (">") void print(String s) - appends the string s with no newline. void endLine() - appends a newline and a user prompt. (to be used in combination with print(String s).) Code in use by: FileTester DiceTester SpellCompositor vers. history: 05.16.00 - Corrected the default button nonsense to just an action of the JTextField 07.20.00 - Declared the project Syntactically Correct as stands. 07.20.00 - added functionality for interaction with other classes. i.e. getLine()/printLine(), etc. 07.20.00 - removed all old debugging code. 07.24.00 - added feature that scrolls viewpane as new text appears. 07.24.00 - integrated BasicWindowMonitor into the class file. 09.27.00 - changed implementation of print statements to differentiate between printing a system prompt or a user prompt, making the system prompt a optional string or no string. Added print(), printEndLine(), endLine(), and newLine(). 06.12.01 - began work on the OSX version of this app. Work needed: - need a method of scripting commands from a file or a series of text inputs or text input format - need accessors to add/remove menuItems - Panel of buttons and lists to clear and to access history - fix the text field to be as large as the width minus the button always. Work Completed: found function (JButton.doClick(int millisecs)) to activate button to prevent redundancy examine focus to be always returned to the text field. -Finished ??.??.?? discover why the default button is not working... -Finished 05.16.00 */ import java.awt.*; import java.awt.event.*; import java.awt.Window; import java.util.*; import java.io.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; public class InteractionFrame extends JFrame { public static final String DEFAULT_SYS_PROMPT = "Sys: "; JScrollPane scrollingRegion; JTextArea textDisplayRegion; JTextField textEntryRegion; JButton executeButton; JPanel commandPanel; JRootPane root; JFileChooser scriptChooser; JFrame frame; JMenuBar menuBar; JMenu fileMenu; JMenuItem aboutBoxMenuItem; JMenuItem quitMenuItem; JMenuItem runScriptMenuItem; JMenu editMenu; JMenuItem undoMenuItem; JMenuItem cutMenuItem; JMenuItem copyMenuItem; JMenuItem pasteMenuItem; JMenu optionsMenu; JCheckBoxMenuItem promptCBMenuItem; File scriptFile; boolean scriptMode; Vector scriptVector; Enumeration scriptEnumeration; Font systemFont; Font inputFont; String commandString; boolean validString; String sysPrompt; boolean sysPromptSwitch; JScrollBar jsb; BoundedRangeModel scrollModel; int oldMax; public InteractionFrame() { super ("Spell Compositor - Arcana SpellBook"); setSize(500, 500); addWindowListener(new BasicWindowMonitor()); frame = this; Insets objectInsets = new Insets(2,2,2,2); setValidString(false); setSysPromptSwitch(false); setSysPrompt(DEFAULT_SYS_PROMPT); scriptChooser = new JFileChooser(); scriptVector = new Vector(); menuBar = new JMenuBar(); menuBar.setBorder(new BevelBorder(BevelBorder.RAISED)); fileMenu = new JMenu("File"); fileMenu.setMnemonic('F'); aboutBoxMenuItem = new JMenuItem("About…"); fileMenu.add(aboutBoxMenuItem); aboutBoxMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { printEndLine("\nThoth Interaction System \n" + "Written by Scott Ziegler \n" + "¢ Simulcra 1999"); } }); runScriptMenuItem = new JMenuItem("Run Script…"); runScriptMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { int option = scriptChooser.showOpenDialog(frame); if (option == JFileChooser.APPROVE_OPTION) { scriptMode = true; scriptFile = scriptChooser.getSelectedFile(); runScript(); } } }); fileMenu.add(runScriptMenuItem); quitMenuItem = new JMenuItem("Quit"); fileMenu.add(quitMenuItem); quitMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(0); } }); editMenu = new JMenu("Edit"); undoMenuItem = new JMenuItem("Undo"); editMenu.add(undoMenuItem); cutMenuItem = new JMenuItem("Cut"); editMenu.add(cutMenuItem); cutMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { //System.exit(0); } }); copyMenuItem = new JMenuItem("Copy"); editMenu.add(copyMenuItem); copyMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { //System.exit(0); } }); pasteMenuItem = new JMenuItem("Paste"); editMenu.add(pasteMenuItem); pasteMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { //System.exit(0); } }); optionsMenu = new JMenu("Options"); promptCBMenuItem = new JCheckBoxMenuItem("Use System Prompt", false); optionsMenu.add(promptCBMenuItem); promptCBMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { setSysPromptSwitch(promptCBMenuItem.getState()); } }); menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(optionsMenu); systemFont = new Font("Courier", Font.PLAIN, 10); inputFont = new Font("Monaco", Font.PLAIN, 9); textDisplayRegion = new JTextArea(); textDisplayRegion.setEditable(false); textDisplayRegion.setLineWrap(true); textDisplayRegion.setWrapStyleWord(true); textDisplayRegion.setFont(systemFont); // textDisplayRegion.setInsets(objectInsets); scrollingRegion = new JScrollPane(textDisplayRegion); scrollingRegion.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // LineBorder line = new LineBorder(Color.black); // WORKS // scrollingRegion.setViewportBorder(line); // WORKS jsb = scrollingRegion.getVerticalScrollBar(); scrollModel = jsb.getModel(); oldMax = 0; scrollModel.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) { BoundedRangeModel sourceModel = (BoundedRangeModel)ce.getSource(); int newMax = sourceModel.getMaximum(); if (newMax > oldMax) { scrollModel.setValue(newMax); oldMax = newMax; } } }); commandPanel = new JPanel(); commandPanel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); executeButton = new JButton("Execute"); executeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { setCommandString(getCommandText()); displayText(); setValidString(true); } }); textEntryRegion = new JTextField(); textEntryRegion.setFont(inputFont); textEntryRegion.setColumns(66); // Code to register the ENTER key to display. textEntryRegion.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { executeButton.doClick(55); // click button for 55 milliseconds } }); c.gridx = 0; c.gridy = 0; c.gridheight = 1; c.gridwidth = 1; c.weightx = c.weighty = 1.0; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.CENTER; c.insets = objectInsets; commandPanel.add(textEntryRegion, c); c.gridx = 1; c.gridy = 0; c.gridheight = 1; c.gridwidth = 1; c.weightx = c.weighty = 0.0; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.CENTER; c.insets = objectInsets; commandPanel.add(executeButton, c); // initialize window, add components, and show setJMenuBar(menuBar); Container progFrameContentPane = getContentPane(); progFrameContentPane.add(commandPanel, BorderLayout.SOUTH); progFrameContentPane.add(scrollingRegion, BorderLayout.CENTER); setVisible(true); textEntryRegion.requestFocus(); initSession(); } public String getCommandString() { return commandString; } public void setCommandString(String s) { commandString = s; } public String getSysPrompt() { return sysPrompt; } public void setValidString(boolean b) { validString = b; } public void setSysPromptSwitch(boolean b) { sysPromptSwitch = b; } public void setSysPrompt(String s) { sysPrompt = s; } public boolean isValidString() { return validString; } public boolean isSysPromptSwitch() { return sysPromptSwitch; } public String getLine() { if (scriptMode) { return getScriptLine(); } String gottenString; while (!isValidString()) {} // Wait for input to be ready (button press) gottenString = getCommandString(); setValidString(false); return gottenString; } private String getScriptLine() { String statement = new String(""); if (scriptEnumeration.hasMoreElements()) { statement = (String)scriptEnumeration.nextElement(); return statement; } else { scriptMode = false; scriptVector.removeAllElements(); return getLine(); // if no more statements, call old getLine() again. } } public void printLine(String s) { if (isSysPromptSwitch()) { textDisplayRegion.append(getSysPrompt()); } textDisplayRegion.append(s); // clearText(); // ??? not needed I think newLine(); textEntryRegion.requestFocus(); } public void printEndLine(String s) { if (isSysPromptSwitch()) { textDisplayRegion.append(getSysPrompt()); } textDisplayRegion.append(s); newLine(); newPrompt(); textEntryRegion.requestFocus(); } public void print(String s) { textDisplayRegion.append(s); } // for combination with print calls public void endLine() { newLine(); newPrompt(); textEntryRegion.requestFocus(); } private String getCommandText() { String s = textEntryRegion.getText(); if (s.length() != 0) return s; else return ""; } private void initSession() { textDisplayRegion.append("Thoth Interaction System \n"); textDisplayRegion.append("Written by Scott Ziegler \n"); textDisplayRegion.append("¢ Simulcra 1999 \n"); textDisplayRegion.append("System Ready."); newLine(); newPrompt(); textEntryRegion.requestFocus(); } private void displayText() { String fieldText = getCommandText(); if (fieldText.length() != 0) { textDisplayRegion.append(fieldText); clearText(); newLine(); } textEntryRegion.requestFocus(); } private void clearText() { textEntryRegion.setText(""); } private void newPrompt() { textDisplayRegion.append(">"); } public void newLine() { textDisplayRegion.append("\n"); } private void runScript() { if (scriptMode) { try { FileReader fr = new FileReader(scriptFile); BufferedReader in = new BufferedReader(fr); String statement = new String(""); statement = in.readLine(); while(!(statement.compareTo("<End>") == 0)) { scriptVector.addElement(statement); statement = in.readLine(); } in.close(); scriptEnumeration = scriptVector.elements(); } catch(IOException ioe) { System.out.println("Error: IO Error! " + ioe.toString()); } } } }